// class SceneTitle extends GuaScene {
//     constructor(game) {
//         super(game)
//         self.game = game
//         game.registerAction('k', function() {
//             var main = new Scene(self.game)
//             game.replaceScene(main)

//         })
//     }


//     draw() {
//         this.game.context.strokeText('开始游戏', 20, 250)
//     }

// }

class Scene extends GuaScene {
    constructor(game) {
        super(game)
        this.collided_action = {}
        this.colliding_collided = {}
        this.setup()
        this.debugModeEnabled = true

    }

    getElementsByName(name) {
        var e = this.elements[name] 
        return e
    }
    setup() {
        var self = this
        this.bg = GuaImage.new(this.game, 'sky')

        this.cloud = Cloud.new(this.game)
        this.player = Player.new(this.game)
        this.player.setPosition(100, 200)


        this.addElement(this.cloud)
        this.addElement(this.player)
        

        var numOfEnemies = 15
        this.addEnemies(numOfEnemies)
        this.setupInputs()
       
    }


    triggerCollideEvent() {
        var collidings = Object.keys(this.colliding_collided)
        for (let i = 0; i < collidings.length; i++) {
            let collidingName = collidings[i]
            var collidingObjects = this.elements[collidingName]
            var collidedObjects = this.getElementsByName(this.colliding_collided[collidingName])
            if (!collidingObjects) {
                continue
            }
            for (var j = 0; j < collidingObjects.length; j++) {
                var colliding = collidingObjects[j]
                for (var k = 0; k < collidedObjects.length; k++) {
                    var collided = collidedObjects[k]
                    if (pointInItem(colliding.image.x, colliding.image.y, collided.image)) {
                        this.collided_action[collidingName](colliding, collided)
                    }
                }
            }
        }
    }

    addEnemies(num) {
        this.enemies = []
        for (var i = 0; i < num; i++) {
            var e = Enemy.new(this.game)
            this.addElement(e)

            // this.registerCollide(function() {
            //     this.enemy.collide()
            // })

            this.enemies.push(e)
        }
    }


    setupInputs() {
        var self = this
        this.game.registerAction('a', function() {
            self.player.moveLeft()
        })
        this.game.registerAction('d', function() {
            self.player.moveRight()
        })
        this.game.registerAction('w', function() {
            self.player.moveUp()
        })
        this.game.registerAction('s', function() {
            self.player.moveDown()
        })
        this.game.registerAction('j', function() {
            self.player.fire()
        })

        self.registerCollideEvent("player", "enemyBullet", function(colliding, collided) {
            colliding.alive = false
            collided.kill()
            var f = new Fire(self.game)
            f.init(colliding.image.x, colliding.image.y)
            self.addElement(f)
        })
        self.registerCollideEvent("enemy", "bullet", function(colliding, collided) {
            colliding.alive = false
            collided.kill()
            var f = new Fire(self.game)
            f.init(colliding.image.x, colliding.image.y)
            self.addElement(f)
        })
        self.registerCollideEvent("enemy", "particle", function(colliding, collided) {
            // 爆炸粒子会伤害其他敌机
            if (!collided.boomed) {
                var f = new Fire(self.game)
                f.init(colliding.image.x, colliding.image.y)
                self.addElement(f)
            }
            colliding.alive = false
            collided.kill()
        }) 
        self.registerCollideEvent("player", "enemy", function(colliding, collided) {
            while (colliding.hp > 0){
                colliding.kill()
                collided.kill()
            }
        }) 
     }


    registerCollideEvent(collided, colliding, callback) {
        // 注册碰撞事件，碰撞体和被碰撞体 一对一关系，目前只支持一对一
        this.colliding_collided[colliding] = collided
        this.collided_action[colliding] = callback
    }
    elementCollide() {

    }

    draw() {
        super.draw()
    }
    update() {
            super.update()
            this.triggerCollideEvent()
        }

}